package com.banshuibao.bsb_android.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.fragment.AskOneFragment;
import com.banshuibao.bsb_android.fragment.ConsultationFragment;

public class TabActivity extends BaseActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {

    private ImageView back;
    private RadioGroup radioGroup;
    private FrameLayout frameLayout;
    private FragmentTransaction transaction;
    private FragmentManager manager;
    private AskOneFragment onOneFragment;
    private ConsultationFragment consultationFragment;
//    private AskFragment askFragment;
    private RadioButton rb_tb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab);
        manager = getSupportFragmentManager();
        initView();
        //判断是否有未采纳的问题 如果有显示快速咨询页面
        int flag = getIntent().getIntExtra("flag", 0);
        if (flag != 0 && flag == 1) {
            rb_tb.setChecked(true);
            showFragment(R.id.rb_tobe);
            showDialog();
        } else {
            showFragment(R.id.rb_one);
        }
    }

    private void showDialog() {
        Dialog alertDialog = new AlertDialog.Builder(context).
                setMessage("您有未采纳答案，请先采纳后方可继续进行快速咨询")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                }).setNegativeButton("", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.cancel();
                            }
                        }).create();
        alertDialog.show();
    }

    private void initView() {
        back = (ImageView) findViewById(R.id.back_tab);
        radioGroup = (RadioGroup) findViewById(R.id.rg);
        frameLayout = (FrameLayout) findViewById(R.id.tab_frame);
        rb_tb = (RadioButton) findViewById(R.id.rb_tobe);
        back.setOnClickListener(this);
        radioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_tab:
                finish();
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.rb_one:
                showFragment(R.id.rb_one);
                break;
            case R.id.rb_tobe:
                showFragment(R.id.rb_tobe);
                break;
//            case R.id.rb_ask:
//                showFragment(R.id.rb_ask);
//                break;
        }
    }

    private void showFragment(int id) {
        transaction = manager.beginTransaction();
        hideFragment(transaction);
        switch (id) {
            case R.id.rb_one:
                if (onOneFragment == null) {
                    onOneFragment = new AskOneFragment();
                    transaction.add(R.id.tab_frame, onOneFragment, "ask");
                } else {
                    transaction.show(onOneFragment);
                }
                break;
            case R.id.rb_tobe:
                if (consultationFragment == null) {
                    consultationFragment = new ConsultationFragment();
                    transaction.add(R.id.tab_frame, consultationFragment, "consultation");
                } else {
                    transaction.show(consultationFragment);
                }
                break;
//            case R.id.rb_ask:
//                if (askFragment == null) {
//                    askFragment = new AskFragment();
//                    transaction.add(R.id.tab_frame, askFragment, "ask");
//                } else {
//                    transaction.show(askFragment);
//                }
//                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction ft) {
        if (onOneFragment != null) {
            ft.hide(onOneFragment);
        }
        if (consultationFragment != null) {
            ft.hide(consultationFragment);
        }
//        if (askFragment != null) {
//            ft.hide(askFragment);
//        }
    }
}
